// 商家详情页面
Page({
  /**
   * 页面的初始数据
   */
  data: {
    businessInfo: {},
    businessType: '',
    businessId: '',
    isLoading: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('商家详情页面加载', options);
    
    if (options.id && options.type) {
      this.setData({
        businessId: options.id,
        businessType: options.type
      });
      this.loadBusinessDetail(options.id, options.type);
    } else {
      wx.showToast({
        title: '参数错误',
        icon: 'none'
      });
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
    }
  },

  /**
   * 加载商家详情
   */
  loadBusinessDetail(id, type) {
    console.log('加载商家详情', id, type);
    
    // 模拟商家数据库
    const businessDatabase = {
      'pet-hospital': {
        1: {
          name: '爱宠宠物医院',
          address: '社区东门200米',
          phone: '400-123-4567',
          time: '09:00-21:00',
          status: '门店实地考察',
          description: '专业宠物医疗服务，设备先进，医生经验丰富',
          services: ['宠物诊疗', '疫苗接种', '绝育手术', '美容护理'],
          rating: 4.8,
          reviews: 156,
          images: [],
          coordinates: {
            latitude: 39.9042,
            longitude: 116.4074
          }
        },
        2: {
          name: '萌宠之家',
          address: '社区西门150米',
          phone: '400-123-4568',
          time: '08:30-20:30',
          status: '门店实地考察',
          description: '温馨的宠物医疗环境，24小时急诊服务',
          services: ['宠物诊疗', '急诊服务', '健康体检', '营养咨询'],
          rating: 4.6,
          reviews: 89,
          images: [],
          coordinates: {
            latitude: 39.9042,
            longitude: 116.4074
          }
        },
        3: {
          name: '宠物乐园',
          address: '社区北门300米',
          phone: '400-123-4569',
          time: '10:00-22:00',
          status: '门店实地考察',
          description: '综合性宠物服务机构，医疗美容一体化',
          services: ['宠物诊疗', '美容护理', '寄养服务', '训练课程'],
          rating: 4.7,
          reviews: 203,
          images: [],
          coordinates: {
            latitude: 39.9042,
            longitude: 116.4074
          }
        }
      },
      'pharmacy': {
        4: {
          name: '健康大药房',
          address: '社区南门100米',
          phone: '400-123-4570',
          time: '07:00-23:00',
          status: '门店实地考察',
          description: '24小时营业，药品齐全，专业药师服务',
          services: ['处方药', '非处方药', '保健品', '医疗器械'],
          rating: 4.5,
          reviews: 342,
          images: [],
          coordinates: {
            latitude: 39.9042,
            longitude: 116.4074
          }
        },
        5: {
          name: '便民药店',
          address: '社区东门300米',
          phone: '400-123-4571',
          time: '08:00-22:00',
          status: '门店实地考察',
          description: '便民服务，价格实惠，常用药品齐全',
          services: ['常用药品', '中药饮片', '保健品', '计生用品'],
          rating: 4.3,
          reviews: 178,
          images: [],
          coordinates: {
            latitude: 39.9042,
            longitude: 116.4074
          }
        }
      },
      'convenience': {
        6: {
          name: '全家便利店',
          address: '社区东门50米',
          phone: '400-123-4572',
          time: '24小时营业',
          status: '门店实地考察',
          description: '24小时营业，商品丰富，服务周到',
          services: ['日用品', '零食饮料', '快餐便当', '生活服务'],
          rating: 4.4,
          reviews: 567,
          images: [],
          coordinates: {
            latitude: 39.9042,
            longitude: 116.4074
          }
        },
        7: {
          name: '7-11便利店',
          address: '社区西门100米',
          phone: '400-123-4573',
          time: '24小时营业',
          status: '门店实地考察',
          description: '国际连锁品牌，商品品质有保障',
          services: ['日用品', '零食饮料', '快餐便当', '生活服务'],
          rating: 4.6,
          reviews: 423,
          images: [],
          coordinates: {
            latitude: 39.9042,
            longitude: 116.4074
          }
        }
      }
    };

    const businessInfo = businessDatabase[type]?.[id];
    
    if (businessInfo) {
      this.setData({
        businessInfo: businessInfo,
        isLoading: false
      });
    } else {
      wx.showToast({
        title: '商家信息不存在',
        icon: 'none'
      });
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
    }
  },

  /**
   * 拨打电话
   */
  makePhoneCall() {
    const phoneNumber = this.data.businessInfo.phone;
    console.log('拨打电话', phoneNumber);
    
    wx.makePhoneCall({
      phoneNumber: phoneNumber,
      success: () => {
        console.log('拨打电话成功');
      },
      fail: (err) => {
        console.error('拨打电话失败:', err);
        wx.showToast({
          title: '拨打失败',
          icon: 'none'
        });
      }
    });
  },

  /**
   * 打开地图导航
   */
  openLocation() {
    const address = this.data.businessInfo.address;
    console.log('打开地图导航', address);
    
    wx.showToast({
      title: '正在打开地图...',
      icon: 'success'
    });
    
    // 模拟打开地图
    setTimeout(() => {
      wx.showModal({
        title: '地图导航',
        content: `是否导航到${address}？`,
        confirmText: '导航',
        success: (res) => {
          if (res.confirm) {
            // 这里可以调用实际的导航API
            wx.showToast({
              title: '正在导航...',
              icon: 'success'
            });
          }
        }
      });
    }, 1000);
  },

  /**
   * 分享商家
   */
  shareBusiness() {
    wx.showActionSheet({
      itemList: ['分享给朋友', '分享到朋友圈', '复制链接'],
      success: (res) => {
        const actions = ['分享给朋友', '分享到朋友圈', '复制链接'];
        const selected = actions[res.tapIndex];
        
        if (selected === '复制链接') {
          this.copyBusinessLink();
        } else {
          wx.showToast({
            title: `正在${selected}...`,
            icon: 'success'
          });
        }
      }
    });
  },

  /**
   * 复制商家链接
   */
  copyBusinessLink() {
    const businessName = this.data.businessInfo.name;
    const link = `https://example.com/business/${this.data.businessId}`;
    
    wx.setClipboardData({
      data: link,
      success: () => {
        wx.showToast({
          title: '链接已复制',
          icon: 'success'
        });
      }
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    const businessName = this.data.businessInfo.name;
    return {
      title: `${businessName} - 社区推荐商家`,
      path: `/pages/resource-navigation/business-detail/index?id=${this.data.businessId}&type=${this.data.businessType}`
    };
  }
});
